package com.bjc.sdx.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bjc.sdx.R;
import com.bjc.sdx.adapter.MyGridAdapter;
import com.bjc.sdx.adapter.MyPagerAdapter;
import com.bjc.sdx.model.Fruit;

import java.util.ArrayList;
import java.util.List;

public class Fragment1 extends Fragment {
    private View view1, view2, view3, view4;
    private ViewPager viewPager;  //对应的viewPager
    private List<View> viewList;//view数组
    private ArrayList<Fruit> mList;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container,false);
        inflater = getLayoutInflater();
        mList = new ArrayList<Fruit>();
        for (int i = 0; i <= 4; i++) {
            Fruit apple = new Fruit("4K", R.drawable.apple);
            Fruit banana = new Fruit("5G", R.drawable.banana);
            Fruit grape = new Fruit("通讯录", R.drawable.grape);
            Fruit strawberry = new Fruit("休眠", R.drawable.strawberry);
            Fruit baiduCloud = new Fruit("百度云盘", R.drawable.baidu_cloud);
            mList.add(apple);
            mList.add(banana);
            mList.add(grape);
            mList.add(strawberry);
            mList.add(baiduCloud);
        }


        // 从布局中拿到viewpage对象
        viewPager = (ViewPager) view.findViewById(R.id.viewpager1);
        view1 = inflater.inflate(R.layout.gridview_layout1, null);
        MyGridAdapter adapter = new MyGridAdapter(inflater, mList);
        GridView gridView1 = view1.findViewById(R.id.gridView2);
        gridView1.setAdapter(adapter);

        view2 = inflater.inflate(R.layout.viewpager_layout2, null);
        view3 = inflater.inflate(R.layout.viewpager_layout3, null);
        view4 = inflater.inflate(R.layout.gridview_layout, null);

        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);
        viewList.add(view4);

        PagerAdapter pagerAdapter = new MyPagerAdapter(viewList);
        viewPager.setAdapter(pagerAdapter);
        return view;
    }

}
